#include "player.h"
#include <list>

Player::Player(void)
: color(0)
{
	//initialize the player
}

void Player::setColor(int newColor)
{
	// set the player's color
	color = newColor;
	return;
}

void Player::setup(Board &gameBoard)
{
	// the user sets up his pieces
	int numIter[12] = {1,1,1,2,3,4,4,4,5,8,6,1};
	int i, j, skipRank, placeRow, placeCol, swapRow, swapCol;
	int inputCh, newGame;
	bool done = false;
	MEVENT event;
	list<Piece> placement;
	for(i = 0; i < 12; i++) {
		// for each type of piece
		for(j = 0; j < numIter[i]; j++) {
			// put the correct number of that piece into the queue used for setup
			placement.push_back(Piece(i, 0, false));
		}
	}
	while(!placement.empty()) {
		// loop unitl all pieces are placed
		// display piece, and buttons to go to the next piece or place them all randomly
		mvprintw(10, 2, "Place Your Piece");
		attron(COLOR_PAIR(color*2 + 1));
		mvprintw(12, 7, "      ");
		mvprintw(13, 7, "  %c   ", placement.back().getShortDescr());
		mvprintw(14, 7, "      ");
		attroff(COLOR_PAIR(color*2 + 1));
		attron(COLOR_PAIR(8));
		mvprintw(16, 4, " Skip Piece ");
		mvprintw(18, 4, "   Random   ");
		attroff(COLOR_PAIR(8));
		
		// get user input
		inputCh = getch();
		if(inputCh == KEY_MOUSE) {
			if(getmouse(&event) == OK) {		  
				if(event.bstate & BUTTON1_CLICKED) {
					if(newGame = gameBoard.checkMenu(event.x, event.y)) {
						// check the regular menu buttons
						if(newGame == 1) {
							// set the New Game flag and leave the user setup
							gameBoard.setNewGame(true);
							return;
						}
					} else if(event.x > -1 + 20 && event.x < 82 + 20 && event.y > 0 && event.y < 41) {
						// if a spot on the board was chosen
						if(event.y%4 != 0 && (event.x-20)%8 != 0 && (event.x-1-20)%8!= 0)	{
							// if the spot was not a line between pieces
							// get the board coordinates based on the screen coordinates
							placeRow = 9-event.y/4;
							placeCol = (event.x - 20)/8;
							if(placeRow < 4) {
								// the user must place the pieces on his half
								if(gameBoard.getPiece(placeRow, placeCol).getRank() == -1) {
									// makes sure the user has not placed a piece there yet
									// place the piece
									gameBoard.setPiece(Posit(placeRow, placeCol), placement.back());
									// remove it from the list
									placement.pop_back();
									// update the board
									gameBoard.printBoard();
								}
							}
						}
					} else if (event.x > 3 && event.x < 17 && event.y == 16) {
						// if the Skip button was pressed
						// save the rank of the pieces we are skipping
						skipRank = placement.back().getRank();
						while(placement.back().getRank() == skipRank) {
							// until a new rank is in the front of the list
							// add the piece to the back of the list
							placement.push_front(placement.back());
							// remove it from the front of the list
							placement.pop_back();
						}
					} else if (event.x > 3 && event.x < 17 && event.y == 18) {
						// if the Random button was pressed
						while(!placement.empty()) {
							// loop through the rest of the list
							do {
								// choose a random row and column until we get an empty one
								placeRow = rand()%4;
								placeCol = rand()%10;
							} while (gameBoard.getPiece(placeRow,placeCol).getRank() >= 0);
							// place the piece
							gameBoard.setPiece(Posit(placeRow, placeCol), placement.back());
							// remove it from the list
							placement.pop_back();
							// update the board
							gameBoard.printBoard();
						}
					}	
				}
			}
		}
	}
	// clear the stuff under the menu
	mvprintw(13, 7, "      ");
	mvprintw(14, 7, "      ");
	mvprintw(16, 4, "            ");
	mvprintw(18, 4, "            ");
	while(!done) {
		// give the user the option to swap his pieces until he is ready
		mvprintw(10, 2, " Swap Positions ");
		attron(COLOR_PAIR(8));
		mvprintw(12, 4, "    Done    ");
		attroff(COLOR_PAIR(8));
		refresh();
		placeRow = -1;
		swapRow = -1;
		// let the user choose a starting piece
		while(placeRow == -1 && !done) {
			gameBoard.printBoard();
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(newGame = gameBoard.checkMenu(event.x, event.y)) {
							// check the menu buttons
							if(newGame == 1) {
								gameBoard.setNewGame(true);
								return;
							}
						} else if(event.x > -1 + 20 && event.x < 82 + 20 && event.y > 24 && event.y < 41) {
							// if the user's half of the board was selected
							if(event.y%4 != 0 && (event.x-20)%8 != 0 && (event.x-1-20)%8!= 0) {
								// if it wasn't a line between squares
								// show the piece as selected
								gameBoard.printPiece(9-event.y/4,(event.x - 20)/8,2);
								refresh();
								// save its position
								placeRow = 9-event.y/4;
								placeCol = (event.x - 20)/8;
							}
						} else if(event.x > 3 && event.x < 17 && event.y == 12) {
							// if the Done button is pressed, finish
							done = true;
						}
					}
				}
			}
			refresh();
		}
		// let the user choose an ending piece
		while(swapRow == -1 && !done) {
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(newGame = gameBoard.checkMenu(event.x, event.y)) {
							// check the menu buttons
							if(newGame == 1) {
								gameBoard.setNewGame(true);
								return;
							}
						} else if(event.x > -1 + 20 && event.x < 82 + 20 && event.y > 24 && event.y < 41) {
							// if the user's half of the board was selected
							if(event.y%4 != 0 && (event.x-20)%8 != 0 && (event.x-1-20)%8!= 0)	{
								// if it wasn't a line between squares
								// save its position 
								swapRow = 9-event.y/4;
								swapCol = (event.x - 20)/8;
								// swap the pieces
								gameBoard.swapPieces(Posit(placeRow, placeCol), Posit(swapRow, swapCol));
							}
						} else if(event.x > 3 && event.x < 17 && event.y == 12) {
							// if the Done button is pressed, finish
							done = true;
						}
					}
				}
			}
		}
	}
	
	// clear the area under the menu buttons
	mvprintw(10, 3, "              ");
	mvprintw(12, 4, "            ");
	refresh();
	return;
}

void Player::makeMove(Board &gameBoard)
{
	// let the player move a piece
	int startRow, startCol, endRow, endCol;
	int inputCh, done;
	MEVENT event;
	Posit start(0, 0), end(0, 0);
	do {
		// loop through until a valid move is made
		startRow = -1;
		endRow = -1;
		// let the user choose a starting piece
		while(startRow == -1) {
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(done = gameBoard.checkMenu(event.x, event.y)) {
							// check the menu buttons
							if(done == 1) {
								gameBoard.setNewGame(true);
								return;
							}
						} else if(event.x > -1 + 20 && event.x < 82 + 20 && event.y > 0 && event.y < 41) {
							// if a space on the board was selected
							if(event.y%4 != 0 && (event.x-20)%8 != 0 && (event.x-1-20)%8!= 0) {
								// if it wasn't a line between squares
								if(gameBoard.getPiece(9-event.y/4, (event.x - 20)/8).getOwner() == 0) {
									// if the piece belongs to the user
									// show it as selected, along with its possible moves
									gameBoard.printPiece(9-event.y/4,(event.x - 20)/8,1);
									refresh();
									// save its position
									startRow = 9-event.y/4;
									startCol = (event.x - 20)/8;
								}
							}
						}
					}
				}
			}
			refresh();
		}
		// let the user choose a starting piece
		while(endRow == -1) {
			gameBoard.printBoard();
			gameBoard.printPiece(startRow,startCol,1);
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(gameBoard.checkMenu(event.x, event.y)) {
							// check the menu buttons
							if(done == 1) {
								gameBoard.setNewGame(true);
								return;
							}
						} else if(event.x > -1 + 20 && event.x < 82 + 20 && event.y > 0 && event.y < 41) {
							// if a space on the board was selected
							if(event.y%4 != 0 && (event.x-20)%8 != 0 && (event.x-1-20)%8!= 0)	{
								// if it wasn't a line between squares
								// save its position
								endRow = 9-event.y/4;
								endCol = (event.x - 20)/8;
							}
						}
					}
				}
			}
		}
		gameBoard.printBoard();

		// set the positions
		start.setRow(startRow);
		start.setCol(startCol);
		end.setRow(endRow);
		end.setCol(endCol);
		// the movePiece function attempts to carry out the move
		// it returns true if successful, false if the move is not legal
	} while(!gameBoard.movePiece(0, start, end));

	return;
}

